const {
    default: Toast
} = require("../../../miniprogram_npm/@vant/weapp/toast/toast");

// packageC/comments/oneComment/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        d_id: '',
        obj: {},
        val: '',
        fileList: [],
        imgs: [],
        resourceURL: wx.api.host + '/',
        order_id:'',
        detail_id:'',
        send_id:'',
        content:""
    },

    // 获取评价商品信息
    getOrderDetail() {
        Toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration: 0,
        })
          if (this.data.detail_id) {
            wx.http.get(wx.api.getorderdetail, {
                order_id: this.data.order_id,
            }).then((res) => {
                if (res.code == 1) {

                    res.data.productinfo[0].detailinfo.forEach((ele)=>{
                        ele.detail_id
                        if (ele.detail_id==this.data.detail_id) {
                            this.setData({
                                obj:ele
                            })
                        } 
                    })
                 
                    setTimeout(() => {
                        Toast.clear()
                    }, 500);
                }
            })
          } else {

            wx.http.get(wx.api.orderserviceinfo, {
                send_id: this.data.send_id,
            }).then((res) => {
                if (res.code == 1) {

                    this.setData({
                        obj: res.data[0]
                    })
                   
                    setTimeout(() => {
                        Toast.clear()
                    }, 500);
                }
            })
          }
     
    },

    // 图片上传
    handleUploader(e) {
        Toast.loading({
            message: '上传中...',
            forbidClick: true,
            duration: 0,
        })
        const {
            file
        } = e.detail;
        let that = this
        
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        wx.uploadFile({
            url: wx.api.uploadshopimage,
            filePath: file.url,
            name: 'image',
            header: {
                "Content-Type": "multipart/form-data",
                "token": wx.getStorageSync('token'),
            },
            formData: {
                type_name: 'comment'
            },
            success: function (res) {
                let _res = JSON.parse(res.data)
                // http://xxxxx.com/xxxxx.png
                const fileList = [...that.data.fileList]
                const url = wx.api.host + '/' + _res.data.file_path
                fileList.push({
                    url
                })
                // /xxxxx.png
                const imgs = [...that.data.imgs]
                imgs.push(_res.data.file_path)
                that.setData({
                    imgs,
                    fileList
                })
                Toast.clear()
            },
        })
    },

    // 删除图片
    handleDelete(e) {
        let _index = e.detail.index
        let fileList = this.data.fileList
        fileList.splice(_index, 1)
        let imgs = this.data.imgs
        imgs.splice(_index, 1)
        this.setData({
            fileList,
            imgs
        })
    },
    onChange(event){
        
        this.setData({
            content:event.detail
        })
    },
    // 提交评价
    onSubmit(event) {
        let form = event.detail.value
        let obj = {
            goods: this.data.obj,
            content: form.contents,
            fileList: this.data.imgs,
        }
        let url=this.data.detail_id?wx.api.usercomment:wx.api.iscomment
        let images = JSON.stringify(this.data.imgs.join(','))
        images=images.replace(/\"/g, "")
     
        wx.http.post(url, {
            handle: "submit", //上传时必填，值为submit
            order_id: this.data.order_id, //订单id
             detail_id: this.data.detail_id?this.data.detail_id:null, //订单详情id
            content: form.contents, //评价内容
            images: images 
        }).then((res) => {
       
            if (res.code == 1) {
                wx.setStorageSync('tempCommentInfo', JSON.stringify(obj))
                wx.redirectTo({
                    url: '/packageB/comments/commentSuccess/index',
                })
            }else{
                Toast.fail(res.msg)
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
     
        this.setData({
            send_id: options.send_id,
            order_id: options.order_id,
            detail_id: options.detail_id,

        })
        this.getOrderDetail()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})